package edu.pitt.dbmi.odie.gapp.gwt.server.navigator;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import edu.pitt.dbmi.odie.gapp.gwt.client.dao.ODIE_DaoAnalysis;
import edu.pitt.dbmi.odie.gapp.gwt.client.navigator.ODIE_NavigatorSelectionService;
import edu.pitt.dbmi.odie.gapp.gwt.model.ODIE_Analysis;
import edu.pitt.dbmi.odie.gapp.gwt.model.ODIE_Document;
import edu.pitt.dbmi.odie.gapp.gwt.server.util.gdata.ODIE_DocsServiceFetcher;
import edu.pitt.dbmi.odie.gapp.gwt.server.util.gdata.ODIE_GDataDocsService;
import edu.pitt.dbmi.odie.gapp.gwt.server.util.marginizer.ODIE_Marginizer;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ODIE_NavigatorSelectionServiceImpl extends RemoteServiceServlet
		implements ODIE_NavigatorSelectionService {
	
	public ODIE_DaoAnalysis fetchCurrentAnalysis() {
		ODIE_DaoAnalysis result = null;
		ODIE_SelectionCacher odieSelectionCacher = ODIE_SelectionCacher
				.fetchOdieSelectionCacher(this);
		if (odieSelectionCacher != null) {
			result = odieSelectionCacher.fetchAnalysisDao() ;
		}
		return result;
	}

	public ODIE_DaoAnalysis selectAnalysis(String id) {
		ODIE_DaoAnalysis result = null;
		ODIE_SelectionCacher odieSelectionCacher = ODIE_SelectionCacher
				.fetchOdieSelectionCacher(this);
		if (odieSelectionCacher != null) {
			odieSelectionCacher.deSelectAnalysis();
			odieSelectionCacher.selectAnalysisById(id);
			result = odieSelectionCacher.fetchAnalysisDao();
		}
		return result;
	}

	public ODIE_DaoAnalysis selectDocument(String odieAnalysisId, String odieId) {
		ODIE_DaoAnalysis result = null;
		String documentContent = "Unable to load document.";
		ODIE_SelectionCacher odieSelectionCacher = ODIE_SelectionCacher
				.fetchOdieSelectionCacher(this);
		ODIE_GDataDocsService docsService = ODIE_DocsServiceFetcher
				.fetchDocsService(this);
		if (odieSelectionCacher != null && docsService != null) {
			odieSelectionCacher.deSelectDocumentForAnalysis(odieAnalysisId);
			odieSelectionCacher.selectDocumentById(odieId);
			ODIE_Document currentDocument = odieSelectionCacher
					.getCurrentDocument();
			if (currentDocument != null) {
				String rawId = currentDocument.getRawId();
				documentContent = docsService.pullDocumentContent(rawId);
				ODIE_Marginizer marginizer = new ODIE_Marginizer();
				marginizer.setInputFileAsString(documentContent);
				marginizer.execute();
				documentContent = marginizer.getOutputFileAsString();
			}
			result = odieSelectionCacher.fetchAnalysisDao();
		}
		return result;
	}

	public ODIE_DaoAnalysis selectAnnotation(String odieAnalysisId,
			String odieDocumentId, String odieId) {
		ODIE_DaoAnalysis result = null;
		ODIE_SelectionCacher odieSelectionCacher = ODIE_SelectionCacher
				.fetchOdieSelectionCacher(this);
		if (odieSelectionCacher != null) {
			odieSelectionCacher.deSelectAnnotationForDocument(odieAnalysisId,
					odieDocumentId);
			odieSelectionCacher.selectAnnotationById(odieId);
			result = odieSelectionCacher.fetchAnalysisDao();
			
		}
		return result;
	}

	public String fetchCurrentDocument() {
		String documentContent = "No document content.";
		ODIE_SelectionCacher odieSelectionCacher = ODIE_SelectionCacher
				.fetchOdieSelectionCacher(this);
		ODIE_GDataDocsService docsService = ODIE_DocsServiceFetcher
				.fetchDocsService(this);
		if (odieSelectionCacher != null && docsService != null) {
			ODIE_Analysis persistedAnalysis = odieSelectionCacher
					.getCurrentAnalysis();
			if (persistedAnalysis != null) {
				ODIE_Document currentDocument = odieSelectionCacher
						.fetchSelectedDocumentForAnalysis(persistedAnalysis
								.getOdieId());
				if (currentDocument != null) {
					String rawId = currentDocument.getRawId();
					documentContent = docsService.pullDocumentContent(rawId);
					ODIE_Marginizer marginizer = new ODIE_Marginizer();
					marginizer.setInputFileAsString(documentContent);
					marginizer.execute();
					documentContent = marginizer.getOutputFileAsString();
				}
			}
		}
		return documentContent;
	}

	

}
